package com.acme.bankapp.domain;

import java.io.Serializable;

import com.acme.bankapp.domain.Account;
import com.acme.bankapp.domain.Gender;
import com.acme.bankapp.domain.CheckingAccount;
import com.acme.bankapp.domain.SavingAccount;

/**
 * The constructor public Client (String startFirstName, String startLastName, Gender gender) is necessary for searching the client in the List of clients
 * in public Client searchClient(String searchFirstName, String searchLastName, Gender gender) method in Bank class. 
 * @author Java Student
 *
 */
@SuppressWarnings("serial")
public class Client implements Serializable {
	transient private Account account;
	private Gender gender;
	private String firstName;
	private String lastName;
	
    public Client (double startBalance, double startOverdraft, String startFirstName, String startLastName, Gender startGender) {
    	try {
    		if ((startOverdraft < 0)||(startBalance < 0))
    			throw new IllegalArgumentException();
    		else {
    			if (startOverdraft != 0)
    	    		this.account = new CheckingAccount(startBalance, startOverdraft);
    	    	else
    	    		this.account = new SavingAccount(startBalance);
    	    	switch (startGender) {
    	    	case MALE: 
    	    		this.gender = Gender.MALE;
    	    		break;
    	    	case FEMALE:
    	    		this.gender = Gender.FEMALE;
    	    		break;
    	    	default:
    	    		break;
    	    	}
    	    	this.firstName = startFirstName;
    	    	this.lastName = startLastName;
    		}
    	}
    	catch (IllegalArgumentException e){
    		System.out.println("There are uncorrect arguments");
    		this.account = null;
    		this.firstName = null;
    		this.lastName = null;
    		this.gender = null;
    	}
    }
    public Client (String startFirstName, String startLastName, Gender gender) {
    	this.firstName = startFirstName;
    	this.lastName = startLastName;
    	this.gender = gender;
    }
    public Account getAccount() {
    	return this.account;
    }
    public void setAccount(Account account) {
    	this.account = account;
    }
    public Gender getGender() {
    	return this.gender;
    }
    public String getFirstName() {
    	return this.firstName;
    }
    public String getLastName() {
    	return this.lastName;
    }
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (gender != other.gender)
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		return true;
	}
	public StringBuffer toStringClient() {
		StringBuffer toStringValue = new StringBuffer();
		return toStringValue.append(this.gender).append(' ').append(this.firstName).append(' ').append(this.lastName);
	}
    
}
